/**
 * 
 */
package com.pos.set;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.pos.YourApplication;
import com.pos.toolbox.CpuManager;
import com.pos.toolbox.file.FilesManager;
import com.pos.toolbox.file.InstallUtils;

import java.io.File;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * @description: app系统环境等设置
 * @author: jacky.cai
 * @time:2016年8月8日上午11:31:37
 */
public class AppSet {
	/**
	 * 简体中文
	 */
	private static final String LANGUAGE_ZH_CN = "zh_cn";
	/**
	 * 繁体中文
	 */
	private static final String LANGUAGE_ZH_TW = "zh_tw";
	/**
	 * 英文
	 */
	private static final String LANGUAGE_EN = "en";
	/**
	 * 是否为测试模式
	 */
	public static boolean testModel = true;
	/**
	 * 版本号
	 */
	private static int versionCode = 0;
	/**
	 * 版本名称
	 */
	private static String versionName;
	/**
	 * mac地址
	 */
	private static String macAddress;
	/**
	 * 公参imei
	 */
	private static String imei;
	/**
	 * 公参sessionid
	 */
	private static String sessionid;
	/**
	 * 暂时存储的activity
	 */
	public static Activity activity = null;

	/**
	 * activity列表
	 */
	public static List<Activity> activityStack = new ArrayList<Activity>();
	/**
	 * activity列表
	 */
	public static List<Activity> cashierActivities = new ArrayList<Activity>();

	/**
	 * 初始化系统环境
	 * 
	 * @param context
	 * @param isDebugModel
	 *            是否为调试状态（开关log）
	 * @param isTestModel
	 *            是否为测试状态（切换地址）
	 */
	public static void initEnvironment(Context context, boolean isDebugModel, boolean isTestModel) {
		Log.init(isDebugModel);
		testModel = isTestModel;
		initVersion(context);
		getLocalMacAddress(context);
		deleteUpdateFile(context);
		FilesManager.openFile(context);
		SDKInitializer.initialize(context);
		SDKInitializer.setCoordType(CoordType.BD09LL);

		Log.e("initEnvironment", "cpu 位数=" + CpuManager.getArchType(context));

	}

	/**
	 * 删除更新文件
	 */
	public static void deleteUpdateFile(Context context) {
		File file = new File(InstallUtils.getApkFilePath(context));
		if (file.exists()) {
			file.delete();
		}

	}

	/**
	 * 初始化系统版本
	 */
	private static void initVersion(Context context) {
		try {
			PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			versionCode = pi.versionCode;
			versionName = pi.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取终端手机mac地址
	 */
	public static String getLocalMacAddress(Context context) {
		if (TextUtils.isEmpty(macAddress)) {
			WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
			WifiInfo info = wifi.getConnectionInfo();
			macAddress = info.getMacAddress();
		}
		return macAddress;
	}

	/**
	 * 获取客户端版本号
	 */
	public static int getVersionCode(Context context) {
		if (versionCode == 0) {
			try {
				PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
				versionCode = pi.versionCode;

			} catch (NameNotFoundException e) {
				e.printStackTrace();
			}
		}

		return versionCode;
	}

	/**
	 * 获取客户端版本号
	 */
	public static String getVersionName(Context context) {
		if (TextUtils.isEmpty(versionName)) {
			try {
				PackageInfo pi = YourApplication.app.getPackageManager().getPackageInfo( YourApplication.app.getPackageName(), 0);
				versionName = pi.versionName;
				android.util.Log.e("TAG", "getVersionName: "+versionName );
			} catch (NameNotFoundException e) {
				e.printStackTrace();
			}
		}

		return versionName;
	}

	/**
	 * 获取语言版本
	 */
	public static String getLanguage(Context context) {
		Locale locale = context.getResources().getConfiguration().locale;
		String language = locale.getLanguage().toLowerCase();
		String country = locale.getCountry().toLowerCase();
		Log.e("language", "language=" + language);
		Log.e("country", "country=" + country);
		if (language.endsWith("zh") || language.endsWith("cn")) {
			if (country.endsWith("tw") || country.endsWith("hk")) {
				return LANGUAGE_ZH_TW;
			} else {
				return LANGUAGE_ZH_CN;
			}
		} else if (language.endsWith("hk") || language.endsWith("tw")) {
			return LANGUAGE_ZH_TW;
		} else if (language.endsWith("gb") || language.endsWith("us")) {
			return LANGUAGE_EN;
		}
		return LANGUAGE_ZH_CN;

	}

	/**
	 * 获取语言版本
	 */
	public static String getImei(Context context) {
		if (TextUtils.isEmpty(imei)) {
			imei = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
		}
		return imei;
	}

	/**
	 * 获取sessionid
	 */
	public static String getSessionid() {
		if (TextUtils.isEmpty(sessionid)) {
			sessionid = System.currentTimeMillis() + "";
		}
		return sessionid;
	}

	/**
	 * 清空sessionid
	 */
	public static void clearSessionid() {
		sessionid = null;
	}

	/**
	 * MD5加密
	 */
	public static String encodeMD5(String strInput) {
		StringBuffer buf = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(strInput.getBytes("UTF-8"));
			byte b[] = md.digest();
			buf = new StringBuffer(b.length * 2);
			for (int i = 0; i < b.length; i++) {
				if (((int) b[i] & 0xff) < 0x10) { /* & 0xff转换无符号整型 */
					buf.append("0");
				}
				buf.append(
						Long.toHexString((int) b[i] & 0xff)); /* 转换16进制,下方法同 */
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return buf.toString();
	}

}
